﻿using System;
using Microsoft.SharePoint;

namespace TemplateLibraryConnector.Utility
{
    public static class SPItemExtensions
    {
        public static T GetValueOrDefault<T>(this SPItem item, string fieldName, T defaultValue)
        {
            try
            {
                var value = (T) item[fieldName];
                if (value != null)
                {
                    return value;
                }
            }
            catch (Exception)
            {
                return defaultValue;
            }
            return defaultValue;
        }

        public static string GetValueOrDefault(this SPItem item, string fieldName, string defaultValue)
        {
            try
            {
                var value = (string) item[fieldName];
                if (!string.IsNullOrEmpty(value))
                {
                    return value;
                }
            }
            catch (Exception)
            {
                return defaultValue;
            }
            return defaultValue;
        }
    }
}